﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

/**
 * 创建时间：2014/12/15 14:49:30
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.Common
{
    public class LocalCacheDataHelper
    {
        /// <summary>
        /// 是否重新加载
        /// </summary>
        public bool IsReload { get; set; }

        /// <summary>
        /// 本地缓存数据
        /// </summary>
        public string LocalCacheData { get; set; }

        public LocalCacheDataHelper()
        {
            this.IsReload = true;
            this.LocalCacheData = string.Empty;
        }

        /// <summary>
        /// 根据Key获取本地缓存的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public void GetDataFromLocalCache(string key, bool isNeedRefresh = true)
        {
            if (!IsolatedStorageSettingHelper.IsolateStorageKeyIsExist(key))
                return;

            string localData = IsolatedStorageSettingHelper.GetIsolateStorageToString(key);
            if (string.IsNullOrWhiteSpace(localData))
                return;

            string localDataDecrypt = AESEncryptionHelper.DecryptAES(localData);
            if (string.IsNullOrWhiteSpace(localDataDecrypt))
                return;

            if (localDataDecrypt.Length <= KEYS.KEY_DATETIME_FORMAT.Length)
                return;


            string cacheDateTime = localDataDecrypt.Substring(0, KEYS.KEY_DATETIME_FORMAT.Length);
            //1个小时内的数据无需刷新
            string currentDateTime = DateTime.Now.AddHours(-1).ToString(KEYS.KEY_DATETIME_FORMAT);
            try
            {
                //需要刷新验证
                if (isNeedRefresh)
                {
                    if (long.Parse(currentDateTime) - long.Parse(cacheDateTime) <= 0)//无需刷新
                    {
                        this.IsReload = false;
                    }
                    if (!NetworkStatusHelper.IsNetworkAvailable && this.IsReload)
                    {
                        //需要刷新缓存但没有网络连接时，仍然使用缓存数据
                        this.IsReload = false;
                    }
                }
                this.LocalCacheData = localDataDecrypt.Substring(KEYS.KEY_DATETIME_FORMAT.Length);

            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, typeof(LocalCacheDataHelper), MethodBase.GetCurrentMethod());
                this.IsReload = true;
            }
        }

        /// <summary>
        /// 存储至本地缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool SetDataToLocalCache(string key, string data)
        {
            if (string.IsNullOrWhiteSpace(data))
                return false;

            string currentDateTime = DateTime.Now.ToString(KEYS.KEY_DATETIME_FORMAT);
            //加密存储
            string dncryptResult = AESEncryptionHelper.EncryptAES(currentDateTime + data);
            return IsolatedStorageSettingHelper.AddIsolateStorageFromObject(key, dncryptResult);
        }
    }
}
